using System;
using System.Web;
using System.Web.Security;
using System.Security.Principal;
using System.Web.UI;
using System.Web.UI.WebControls;
using BizTown;
using Neo.Core;
using Neo.SqlClient;
using System.Drawing;

public partial class admin_actions_trigger_paycheckdirectdeposit : BiztownCustomPage
{
    protected void Page_Init(object sender, EventArgs e)
    {
        ObjectContext neoContext = new ObjectContext(DataHelper.GetDataStore());
        PaycheckDirectDeposit PaycheckDirectDeposit = new PaycheckDirectDepositFactory(neoContext).FindUnique("EntryId={0}", 1);
        if (PaycheckDirectDeposit.HasBeenTriggered)
        {
            B_TriggerPaycheckDD.Enabled = false;
            L_TriggerAlreadySet.Visible = true;
        }
        BankInfo bankInfo = new BankInfoFactory(neoContext).FindUnique("BankInfoId={0}", 1);
     }


    protected void DepositPayrollDirectDeposit()
    {
        ObjectContext neoContext = new ObjectContext(DataHelper.GetDataStore());
        int payPeriod = 2;
        StudentList studentList = new StudentFactory(neoContext).FindAllObjects();
        foreach (Student student in studentList)
        {
            if (student.BusinessJobId != 0 & student.LastName != null & student.StudentId < 901)
            {
                try
                {
                    // PersonalCheckingDeposit entries are strictly used for Payroll deposits
                    PersonalCheckingDeposit personalCheckingDeposit = new PersonalCheckingDepositFactory(neoContext).CreateObject();
                    personalCheckingDeposit.StudentId = student.StudentId;
                    personalCheckingDeposit.GrossDepositAmount = student.BusinessJob.JobFunction.NetPayAmount;
                    personalCheckingDeposit.PayPeriod = payPeriod;
                    personalCheckingDeposit.CashBackAmount = 0M;
                    neoContext.SaveChanges();
                }
                catch (Exception exception)
                {
                }
            }
        }
    }


    protected void TriggerPayrollDirectDeposit(object sender, EventArgs e)
    {
        DepositPayrollDirectDeposit();

        ObjectContext neoContext = new ObjectContext(DataHelper.GetDataStore());
        PaycheckDirectDeposit PaycheckDirectDeposit = new PaycheckDirectDepositFactory(neoContext).FindUnique("EntryId={0}", 1);
        PaycheckDirectDeposit.HasBeenTriggered = true;
        neoContext.SaveChanges();
        Response.Redirect("trigger_second_payroll_directdeposit.aspx?triggered=true", false);
    }
}
